package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.OrderNumberListDto;
import com.igeekhome.mapper.DNetworkMapper;
import com.igeekhome.mapper.DOrderNumberListMapper;
import com.igeekhome.mapper.WWaybillRuleMapper;
import com.igeekhome.pojo.*;
import com.igeekhome.service.IOrderNumberListService;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.WaybillUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 陈维豪
 */
@Service("orderNumberListService")
public class OrderNumberListServiceImpl implements IOrderNumberListService {

    @Autowired
    private DOrderNumberListMapper dOrderNumberListMapper;

    @Autowired
    private WWaybillRuleMapper waybillRuleMapper;

    @Value("${WAYBILL_RULE_Id}")
    private Integer WAYBILL_RULE_Id;

    @Override
    public PageInfo<OrderNumberListDto> select(Integer page, Integer limit,DOrderNumberList orderNumberList,String searchTime) {

        String date1 = null;
        String date2 = null;
        System.out.println(searchTime);
        System.out.println(orderNumberList.getParentNetworkId());
        if(!CommonUtil.isEmpty(searchTime)){
            String[] split = searchTime.split(" - ");
            date1 = split[0];
            date2 = split[1];
        }

        List<OrderNumberListDto> orderNumberListDtos = dOrderNumberListMapper.listOrderNumberListDto(orderNumberList, date1, date2);
        PageHelper.startPage(page, limit);
        PageInfo<OrderNumberListDto> pageInfo = new PageInfo(orderNumberListDtos);
        return pageInfo;
    }

    @Override
    public Integer saveMethod(DOrderNumberList orderNumberList) {

        //计算出发放的最大单号为多少
        Long endOrderNumber = WaybillUtils.getEndOrderNumber(orderNumberList);
        orderNumberList.setEndOrderNumber(endOrderNumber);

        //通过查找正在使用的规则查看发放的单号是否过多超出范围
        //查找当前正在使用的单号规则
        WWaybillRuleExample we = new WWaybillRuleExample();
        we.createCriteria().andUsageStatusEqualTo("1").andTypeIdEqualTo(WAYBILL_RULE_Id);
        List<WWaybillRule> wWaybillRules = waybillRuleMapper.selectByExample(we);
        WWaybillRule wWaybillRule = wWaybillRules.get(0);

        //通过上面查找到的数据判断发放的单号是否超出范围
        boolean flag = WaybillUtils.checkIfAllowNumber(wWaybillRule, endOrderNumber);

        if(flag){
            //将发放的单号存入单号发放表
            dOrderNumberListMapper.insertSelective(orderNumberList);

            //将发放的最大单号数据存入上面查找好的单号规则（更新单号规则）
            wWaybillRule.setMaxWayBillId(Integer.valueOf(endOrderNumber.toString())+1);
            waybillRuleMapper.updateByPrimaryKeySelective(wWaybillRule);
            return  1;
        }else {
            return  0;
        }

    }

    @Override
    public void updateMethod(DOrderNumberList orderNumberList) {
        dOrderNumberListMapper.updateByPrimaryKeySelective(orderNumberList);
    }

    @Override
    public ResultBean deleteMethod(String idsStr) {
        if(idsStr.contains(",")){
            idsStr = idsStr.substring(0,idsStr.length()-1);
        }
        int delete = dOrderNumberListMapper.deleteByIdStr(idsStr);
        if(delete > 0){
            return ResultBean.success();
        }else {
            return ResultBean.fail("删除失败");
        }
    }

    @Override
    public void changeUse(DOrderNumberList orderNumberList) {

        DOrderNumberListExample orderNumberListExample = new DOrderNumberListExample();
        orderNumberListExample.createCriteria().andParentNetworkIdEqualTo(orderNumberList.getParentNetworkId())
                .andServiceConditionEqualTo(1);

        DOrderNumberList ol = new DOrderNumberList();
        ol.setServiceCondition(0);
        dOrderNumberListMapper.updateByExampleSelective(ol,orderNumberListExample);
        dOrderNumberListMapper.updateByPrimaryKeySelective(orderNumberList);
    }


    @Override
    public Long getWaybillNumber(Integer networkId) {
        DOrderNumberListExample de = new DOrderNumberListExample();
        de.createCriteria().andServiceConditionEqualTo(1).andParentNetworkIdEqualTo(networkId);
        List<DOrderNumberList> orderNumberLists = dOrderNumberListMapper.selectByExample(de);
        System.out.println("orderNuberLists"+orderNumberLists);
        DOrderNumberList orderNumberList = orderNumberLists.get(0);
        System.out.println("orderNumberList"+orderNumberList);
        Long useMaxOrderNumber = orderNumberList.getUseMaxOrderNumber();
        System.out.println("useMaxOrderNumber"+useMaxOrderNumber);
        Long endOrderNumber = orderNumberList.getEndOrderNumber();
        System.out.println("endOrderNumber"+endOrderNumber);
        if(useMaxOrderNumber==null){
            System.out.println("执行1");
            Long startOddId = orderNumberList.getStartOddId();
            return startOddId;
        }else if(useMaxOrderNumber.equals(endOrderNumber)){
            System.out.println("执行2");
            return null;
        }else {
            System.out.println("执行3");
            return (useMaxOrderNumber + 1);
        }

    }

    @Override
    public Integer saveUseWaybill(Integer networkId,Integer waybillNumber) {

        DOrderNumberListExample de = new DOrderNumberListExample();
        de.createCriteria().andServiceConditionEqualTo(1).andParentNetworkIdEqualTo(networkId);

        DOrderNumberList orderNumberList = new DOrderNumberList();
        orderNumberList.setUseMaxOrderNumber(waybillNumber.longValue());

        int i = dOrderNumberListMapper.updateByExampleSelective(orderNumberList, de);
        return i;
    }
}
